package com.daka.controller;

import com.daka.entity.Activity;
import com.daka.entity.ActivitySign;
import com.daka.entity.Member;
import com.daka.enumerate.StatusCode;
import com.daka.service.ActivityService;
import com.daka.service.ActivitySignService;
import com.daka.service.MemberService;
import com.daka.tools.Page;
import com.daka.tools.ServiceRuntimeException;
import com.daka.vo.request.ActivityVo;
import com.daka.vo.request.MemberVo;
import com.daka.vo.request.OrganizationVo;
import com.daka.vo.response.ActivityResponseVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 15:16
 */

@Controller
@RequestMapping("/activity")
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivitySignService activitySignService;
    @Autowired
    private MemberService memberService;
//    @Autowired
//    private OrganizationService organizationService;


    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryList(@RequestBody OrganizationVo vo){
        if(vo.getGroupCode()==null ){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        Page<ActivityResponseVo> page = activityService.selectByGroup(vo.getGroupCode(), vo.getPageNo());
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),page);
    }


    @ResponseBody
    @RequestMapping(value = "/queryMyList" ,method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryMyList(@RequestBody MemberVo vo){

        Page<ActivityResponseVo> page = activityService.selectByMember(vo.getMemberID(), vo.getPageNo());
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),page);
    }


    @ResponseBody
    @RequestMapping(value = "/queryDetail",method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryDetail(@RequestBody ActivityVo vo){
        if(vo.getActivityId()==null ){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        Activity activity = activityService.selectByPrimaryKey(Integer.valueOf(vo.getActivityId()));
//        //组织信息
//        if(activity.getOrganization()==null){
//            throw  new ServiceRuntimeException(StatusCode.ORGANIZATION_NOT_EXISTS.getCode(),StatusCode.ORGANIZATION_NOT_EXISTS.getMessage());
//        }
//        Organization organization = organizationService.selectByPrimaryKey(activity.getOrganization());

        ActivityResponseVo activityResponseVo = ActivityResponseVo.buildVO(activity);

        //志愿者参加本活动的公益时间
        if(vo.getMemberID()!=null){
            Member member = memberService.selectByMemberId(vo.getMemberID());
            ActivitySign activitySign = activitySignService.findByActivityAndVolunteer(activity.getId(),member.getId());
            activityResponseVo.setVolunteerTime(activitySign.getVolunteerTime());
        }
        return new  DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),activityResponseVo);
    }

    @ResponseBody
    @RequestMapping(value = "/test" ,method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> test(@RequestBody MemberVo vo){

        if(1==1){
            throw new ServiceRuntimeException(123,"1312131");
        }
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }

}


